import javax.swing.JOptionPane;

/* This PAC agent adds the ability to check for currently running stories, and provide a connection to them.
 *  It uses the IPv4Wrapper and the NetCommunicationWrapper to look for story host computers, print the list,
 *  and return a connection to one, if selected.
 */
public class AvailableStoriesAgent {

	// Abstraction component data of this PAC agent. This model is a list of available computers.
	String[] storyComputers;

	// Control component of the AvailableStoriesModule PAC agent.
	// Controls getting a list of story host computers (abstraction) and printing out their story titles (presentation).
	public int displayRunningStories() {
		storyComputers = getStoryComputers();
		int room=0;
		if(storyComputers == null)
		{
			JOptionPane.showMessageDialog(null, "There are currently no running stories.", "Error", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
		if(storyComputers[0] == null)
		{
			JOptionPane.showMessageDialog(null, "There are currently no running stories.", "Error", JOptionPane.ERROR_MESSAGE);
			return 0;
		}

		printRunningStories(getStoryNames());
		try
		{
			room = Integer.parseInt( JOptionPane.showInputDialog(null,"Enter the room number you wish to join!","Room Number?",JOptionPane.PLAIN_MESSAGE));
		}
		catch(Exception e)
		{
			displayRunningStories();
		}
		return room;
	}

	// One of the three methods that make up the abstraction component of the AvailableStoriesModule PAC agent, along with the storyComputers data.
	// Gets a list of story host computers
	private String[] getStoryComputers() {
		String[] computerList = IPv4Wrapper.checkForComputers();
		return IPv4Wrapper.checkComputersForRunningStories(computerList);
	}

	// One of the three methods that make up the abstraction component of the AvailableStoriesModule PAC agent, along with the storyComputers data.
	// Gets the names of all of the stories running on the storyComputers.
	private String[] getStoryNames()
	{
		String[] storyNames = new String[storyComputers.length];
		for(int i = 0; i < storyComputers.length; i++)
		{
			if(storyComputers[i] != null)
			{
				NetCommunicationWrapper conn = new NetCommunicationWrapper();
				conn.connect(storyComputers[i]);
				storyNames[i] = conn.getStoryName();
				conn.close();
			}
		}
		return storyNames;
	}

	// Presentation component of the AvailableStoriesModule PAC agent.
	// Prints a numbered list of the running stories
	private void printRunningStories(String[] storyNames)
	{
		String runningList="\nThe following stories are open:\n";
		for(int i = 0; i < storyNames.length; i++)
		{
			if(storyNames[i] != null) {
				runningList = runningList + (i+1) + ". " + storyNames[i]+"\n";
			}
		}
		JOptionPane.showMessageDialog(null, runningList, "Open Stories", JOptionPane.INFORMATION_MESSAGE);
	}

	// One of the three methods that make up the abstraction components of the AvailableStoriesModule PAC agent, along with the storyComputers data.
	// Returns a NetCommunicationWrapper object that is connected to the selected story.
	public NetCommunicationWrapper getStoryConnection(int storyIndex)
	{
		NetCommunicationWrapper conn = new NetCommunicationWrapper();
		conn.connect(storyComputers[storyIndex-1]);	//The printed list of stories from printRunningStories is numbered beginning at 1. 1 must be subtracted to get the storyComputers index.
		return conn;
	}
}